package com.example.helloword;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

public class NestedScrollLayoutTest extends NestedScrollView {
    private ViewGroup contentView;
    public NestedScrollLayoutTest(Context context) {
        super(context);
        init();
    }

    public NestedScrollLayoutTest(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NestedScrollLayoutTest(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public NestedScrollLayoutTest(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
    }

    private String mText = "String";
    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        float baseline;
        float x = getWidth()/2;
        //绘制文字
        Paint paint = new Paint();
        canvas.drawText(mText, x, 100, paint);
        //方法一
        paint.setTextAlign(Paint.Align.CENTER);
        //方法二
        float width = paint.measureText(mText);
        x = getWidth()/2 - width/2;
        canvas.drawText(mText, x, 100, paint);
        //把文字绘制在View中心
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        baseline = (getHeight()/2) + (fontMetrics.descent - fontMetrics.ascent)/2 - fontMetrics.descent;
        //== Height/2 - (descent + ascent)/2
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() > 0 && getChildAt(0) instanceof ViewGroup) {
            ViewGroup firstChild = (ViewGroup) getChildAt(0);
            if (firstChild.getChildCount() > 1) {
                contentView = (ViewGroup) firstChild.getChildAt(1);
            }
        }
    }

    @Override
    public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        // 父View优先滑动拦截机制
        if (dy != 0) {
            // 计算父View可以滑动的距离
            int parentScrollY = getScrollY();
            int parentMaxScrollY = getChildAt(0).getHeight() - getHeight();
            
            if (dy > 0) {
                // 向下滑动（向上滚动内容）
                if (parentScrollY < parentMaxScrollY) {
                    // 父View还能向下滑动，优先拦截
                    int parentCanScroll = Math.min(dy, parentMaxScrollY - parentScrollY);
                    scrollBy(0, parentCanScroll);
                    consumed[1] = parentCanScroll; // 告诉子View父View已经消耗了这些距离
                    return; // 父View拦截了，子View不需要再处理
                }
            } else {
                // 向上滑动（向下滚动内容）
                if (parentScrollY > 0) {
                    // 父View还能向上滑动，优先拦截
                    int parentCanScroll = Math.max(dy, -parentScrollY);
                    scrollBy(0, parentCanScroll);
                    consumed[1] = parentCanScroll; // 告诉子View父View已经消耗了这些距离
                    return; // 父View拦截了，子View不需要再处理
                }
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 调整contentView的高度为父容器高度，使之填充布局，避免父容器滚动后出现空白
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (contentView != null) {
            ViewGroup.LayoutParams lp = contentView.getLayoutParams();
            lp.height = getMeasuredHeight();
            contentView.setLayoutParams(lp);
        }
    }
}
